package com.yishu.servlet;

import com.yishu.Dao.UserDao;
import com.yishu.util.DBConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

public class UpdateStatusServlet extends HttpServlet {

    UserDao userDao = new UserDao();
    DBConnection dbConnection = new DBConnection();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int userId = Integer.parseInt(req.getParameter("userId"));
        int status = Integer.parseInt(req.getParameter("status"));
        int result = 0;

        if(status == 1) {
            status = 0;
            try {
                Connection connection = dbConnection.getCon();
                result = userDao.updateStatus(connection,userId,status);
                dbConnection.closeCon(connection);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            status = 1;
            try {
                Connection connection = dbConnection.getCon();
                result = userDao.updateStatus(connection,userId,status);
                dbConnection.closeCon(connection);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(result == 1) {
            req.setAttribute("result","状态更新成功");
            req.getRequestDispatcher("updateStatusResult.jsp").forward(req,resp);
        }else {
            req.setAttribute("result","状态更新失败");
            req.getRequestDispatcher("updateStatusResult.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
